﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Text;
using E4D.AppFabric.Entities.Cache_Host;
using MDCAdminTool;
using Microsoft.ApplicationServer.Caching.Commands;

namespace E4D.AppFabric.Entities
{
    public static class CacheHostEx
    {
        public static Collection<HostCacheStats> BuildHostCacheStats( this Collection<PSObject> r )
        {
            var result = new Collection<HostCacheStats>();

            if ( r != null )
                foreach ( PSObject o in r )
                {
                    result.Add( ConvertPSObjectToHostCacheStats( o ) );
                }
            return result;
        }

		public static ClusterHealthStatus ConvertPSObjectToClusterHealthStatus(this PSObject o)
		{
			var health = (ClusterHealth)o.BaseObject;

			var retval = new ClusterHealthStatus {Hosts = new List<HostHealthStatus>()};
			
			foreach (var hostHealth in health.Hosts)
			{
				var host = new HostHealthStatus()
				           	{
				           		HostName = hostHealth.HostName,
								Caches = new List<CacheHealthStatus>()
				           	};
				foreach (var namedCache in hostHealth.NamedCaches)
				{
					var cache = new CacheHealthStatus();
					cache.CacheName = namedCache.Name;
					cache.Healthy = (long)namedCache["Healthy"];
					cache.Throttled = (long) namedCache["Throttled"];
					cache.NotPrimary = (long) namedCache["NotPrimary"];
					cache.NoWriteQuorum = (long) namedCache["NoWriteQuorum"];
					cache.UnderReconfiguration = (long) namedCache["UnderReconfiguration"];

					host.Caches.Add(cache);
				}

				retval.Hosts.Add(host);
			}
			return retval;
		}

        public static HostCacheStats ConvertPSObjectToHostCacheStats( this PSObject o )
        {
            return
                new HostCacheStats
                {
                    ItemCount       = (long)o.Properties["ItemCount"].Value ,
                    MissCount       = (long)o.Properties["MissCount"].Value ,
                    NamedCacheCount = (long)o.Properties["NamedCacheCount"].Value ,
                    RegionCount     = (long)o.Properties["RegionCount"].Value ,
                    RequestCount    = (long)o.Properties["RequestCount"].Value ,
                    Size            = (long)o.Properties["Size"].Value
                };
        }
        public static HostConfiguration ConvertPSObjectToHostConfig( this PSObject o )
        {
            return
                new HostConfiguration
                {
                    HostName      = (string)o.Properties["HostName"].Value ,
                    HighWatermark = (long)o.Properties["HighWatermark"].Value ,
                    LowWatermark  = (long)o.Properties["LowWatermark"].Value ,
                    IsLeadHost    = (bool)o.Properties["IsLeadHost"].Value ,
                    CachePort     = (int)o.Properties["CachePort"].Value ,
                    ClusterPort   = (int)o.Properties["ClusterPort"].Value ,
                    ArbitratorPort  = (int)o.Properties["ArbitrationPort"].Value,
                    ReplicationPort = (int)o.Properties["ReplicationPort"].Value,
                    ServiceName   = (string)o.Properties["ServiceName"].Value ,
                    Size          = (long)o.Properties["Size"].Value
                };
        }
        public static Dictionary<string , object> InitNewCacheParam( this MdcCache cache )
        {
            return new Dictionary<string , object>
                     {
                         {"CacheName"    , cache.Name},
                         {"Secondaries"  , cache.IsHighAvailability ? 1 : 0 },
                         {"Eviction"     , cache.Eviction},
                         {"Expirable"    , cache.IsExpirable},
                         {"TimeToLive"   , cache.TTL },
                         {"NotificationsEnabled", cache.NotificationsEnabled}
                     };
        }
    }
}
